package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity(name = "roles")
public class RoleEntity extends DataObjectEntity implements Serializable {

    private String roleName;
    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "roles")
    private Collection<PersonEntity> users = new ArrayList<PersonEntity>();
    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private Collection<RightEntity> rights = new ArrayList<RightEntity>();

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public Collection<PersonEntity> getUsers() {
        return users;
    }

    public void setUsers(Collection<PersonEntity> Users) {
        this.users = Users;
    }

    public Collection<RightEntity> getRights() {
        return rights;
    }

    public void setRights(Collection<RightEntity> rights) {
        this.rights = rights;
    }

    public RoleEntity() {
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.getRoleName() != null ? this.getRoleName().hashCode()
                : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

// TODO: Warning - this method won't work in the case the groupNamefields are not set
        if (!(object instanceof RoleEntity)) {
            return false;
        }
        RoleEntity other = (RoleEntity) object;
        if (this.getRoleName() != other.getRoleName()
                && (this.getRoleName() == null
                || !this.getRoleName().equals(other.getRoleName()))) {
            return false;
        }
        return true;
    }

    @Override
    public String childToString() {
        return "RoleEntity{" + "roleName=" + roleName + ", users=" + users + ", rights=" + rights + '}';
    }

    @Override
    public String toString() {
        return "entity.Group[id="
                + getRoleName() + "]";
    }
}